/*
 * Copyright (c) 2008, Xiong Yingfei, the University of Tokyo All rights
 * reserved.
 */
package ac.jp.u_tokyo.SyncLib;

import java.util.List;

import ac.jp.u_tokyo.SyncLib.util.Helper;
import static ac.jp.u_tokyo.SyncLib.util.Helper.*;

public class EqualSync<T> extends StatelessSync
{
	PrimMod<T> _theMod;

	public EqualSync(T v)
	{
		_theMod = new PrimMod<T>(v);
	}

	public int getParaCount()
	{
		return 1;
	}

//	public Object[] getInitialValues()
//	{
//		return new Object[] {_theMod.getNewValue()};
//	}

	public Mod[] resynchronize(Object[] values, Mod[] modifications)
			throws SynchronizationFailedException
	{
		Object value = apply(modifications[0], values[0]);
		Mod newMod;
		if (Helper.areEqual(value, _theMod.getNewValue()))
			newMod = NullMod.INSTANCE;
		else
			newMod = _theMod;
		
		if (conflict(newMod, modifications[0])) {
			throw new SynchronizationFailedException();
		}
		else
			return new Mod[] {newMod.merge(modifications[0])};
	}

	public Mod[] synchronize(Mod[] modifications)
			throws SynchronizationFailedException
	{
		if (!conflict(_theMod, modifications[0]))
		{
			return modifications;
		}
		else {
			throw new SynchronizationFailedException();
		}
	}
	
	@Override
	public String toString()
	{
		return "Equal[" + _theMod.getNewValue() + "]";
	}

}
